package com.alisls.demo.springcloud.service.user.service.impl.employee;

import com.alisls.demo.springcloud.common.core.util.IdWorker;
import com.alisls.demo.springcloud.service.user.dao.employee.EmployeeDAO;
import com.alisls.demo.springcloud.service.user.dao.user.UserDAO;
import com.alisls.demo.springcloud.service.user.dto.employee.EmployeeDTO;
import com.alisls.demo.springcloud.service.user.dto.user.UserDTO;
import com.alisls.demo.springcloud.service.user.entity.EmployeeDO;
import com.alisls.demo.springcloud.service.user.entity.UserDO;
import com.alisls.demo.springcloud.service.user.service.employee.EmployeeService;
import com.alisls.demo.springcloud.service.user.service.user.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 员工服务
 *
 * @author Ke Wang
 * @date 2020/10/30
 */
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private IdWorker idWorker;

    @Resource
    private UserService userService;

    @Resource
    private EmployeeDAO employeeDAO;

    @Resource
    private UserDAO userDAO;

    @Override
    public EmployeeDTO save(EmployeeDTO employeeDTO) {
        EmployeeDO employeeDO = new EmployeeDO();
        // 将employDTO中的属性，包含子类的属性，拷贝到employeeDO中
        BeanUtils.copyProperties(employeeDTO, employeeDO);
        Long employeeId = idWorker.nextId();
        employeeDO.setId(employeeId);
        employeeDTO.setId(employeeId);

        List<UserDTO> userDTOs = employeeDTO.getUsers();
        if (userDTOs != null) {
            List<UserDO> userDOs = new ArrayList<>();
            for (UserDTO userDTO: userDTOs) {
                UserDO userDO = new UserDO();
                BeanUtils.copyProperties(userDTO, userDO);
                Long userId = idWorker.nextId();
                userDO.setId(userId);
                userDO.setPassword(userService.getDefaultPassword());

                userDTO.setId(userId);

                userDOs.add(userDO);

                userDAO.save(userDO);
            }
            employeeDO.setUsers(userDOs);
        }

        employeeDO = employeeDAO.save(employeeDO);

        return employeeDTO;
    }

    @Override
    public EmployeeDTO getByEmployeeNo(String employeeNo) {
        EmployeeDO employeeDO = employeeDAO.findByEmployeeNo(employeeNo);
        if (employeeDO == null) {
            return null;
        }

        EmployeeDTO employeeDTO = new EmployeeDTO();
        BeanUtils.copyProperties(employeeDO, employeeDTO);

        List<UserDO> userDOs = employeeDO.getUsers();
        if (userDOs != null) {
            List<UserDTO> userDTOs = new ArrayList<>();
            for (UserDO userDO: userDOs) {
                UserDTO userDTO = new UserDTO();
                BeanUtils.copyProperties(userDO, userDTO);
                userDTOs.add(userDTO);
            }
            employeeDTO.setUsers(userDTOs);
        }

        return employeeDTO;
    }

}
